package cn.slipi.admin.controller;

import cn.slipi.admin.biz.PermissionBiz;
import cn.slipi.admin.biz.vo.permission.AuthPermissionVo;
import cn.slipi.admin.biz.vo.permission.PermissionModuleVo;
import cn.slipi.admin.biz.vo.permission.PermissionVo;
import cn.slipi.admin.biz.vo.permission.RoleVo;
import cn.slipi.admin.common.annotation.Permission;
import cn.slipi.admin.common.dto.PageDTO;
import cn.slipi.admin.common.response.JsonResponse;
import cn.slipi.admin.constants.AdminConstants;
import cn.slipi.admin.constants.permission.PermissionConstants;
import cn.slipi.admin.controller.form.permission.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 权限接口
 */


@Validated
@Controller
@ResponseBody
@RequestMapping(AdminConstants.CAR_API_PREFIX + "/permission")
public class PermissionController {

    @Autowired
    private PermissionBiz permissionBiz;

    /**
     * 权限列表授权使用
     *
     * @param rid
     * @return
     */
    @GetMapping("/list/auth/{rid}")
    @Permission(value = {PermissionConstants.PERMISSION_GET_PERMISSION_LIST_FOR_AUTH})
    public JsonResponse<List<AuthPermissionVo>> getPermissionListForAuth(@PathVariable("rid") Integer rid) {
        JsonResponse<List<AuthPermissionVo>> responseVo = new JsonResponse<>();
        List<AuthPermissionVo> result = this.permissionBiz.getPermissionListAuth(rid);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 获取权限列表
     *
     * @param permissionQueryForm
     * @return
     */
    @PostMapping("/p/list")
    @Permission(value = {PermissionConstants.PERMISSION_GET_PERMISSION_LIST})
    public JsonResponse<PageDTO<PermissionVo>> getPermissionList(@Valid @RequestBody PermissionQueryForm permissionQueryForm) {
        JsonResponse<PageDTO<PermissionVo>> responseVo = new JsonResponse<>();
        PageDTO<PermissionVo> result = this.permissionBiz.getPermissionList(permissionQueryForm);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 获取用户权限列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/p/u/list/{userId}")
    @Permission(value = {PermissionConstants.PERMISSION_GET_PERMISSION_LIST_BY_USER_ID})
    public JsonResponse<List<PermissionVo>> getPermissionListByUserId(@Valid @PathVariable Integer userId) {
        JsonResponse<List<PermissionVo>> responseVo = new JsonResponse<>();
        List<PermissionVo> result = this.permissionBiz.getPermissionListByUserId(userId);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 获取权限模块列表
     *
     * @param moduleQueryForm
     * @return
     */
    @PostMapping("/pm/list")
    @Permission(value = {PermissionConstants.PERMISSION_GET_PERMISSION_MODULE_LIST})
    public JsonResponse<PageDTO<PermissionModuleVo>> getPermissionModuleList(@Valid @RequestBody ModuleQueryForm moduleQueryForm) {
        JsonResponse<PageDTO<PermissionModuleVo>> responseVo = new JsonResponse<>();
        PageDTO<PermissionModuleVo> result = this.permissionBiz.getPermissionModuleList(moduleQueryForm);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 获取权限模块列表
     *
     * @return
     */
    @GetMapping("/pm/drop_list")
    @Permission(value = {PermissionConstants.PERMISSION_GET_PERMISSION_MODULE_DROP_LIST})
    public JsonResponse<List<PermissionModuleVo>> getPermissionModuleDropList() {
        JsonResponse<List<PermissionModuleVo>> responseVo = new JsonResponse<>();
        List<PermissionModuleVo> result = this.permissionBiz.getPermissionModuleDropList();
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 添加模块
     *
     * @param moduleSaveForm
     * @return
     */
    @PostMapping("/pm/add")
    @Permission(value = {PermissionConstants.PERMISSION_ADD_MODULE})
    public JsonResponse<Integer> addModule(@Valid @RequestBody ModuleSaveForm moduleSaveForm) {
        JsonResponse<Integer> responseVo = new JsonResponse<>();
        Integer result = this.permissionBiz.addModule(moduleSaveForm);
        responseVo.setData(result);
        return responseVo;
    }


    /**
     * 修改模块
     *
     * @param mid
     * @param moduleSaveForm
     * @return
     */
    @PutMapping("/pm/update/{mid}")
    @Permission(value = {PermissionConstants.PERMISSION_UPDATE_MODULE})
    public JsonResponse<Boolean> updateModule(@PathVariable("mid") Integer mid, @Valid @RequestBody ModuleSaveForm moduleSaveForm) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.permissionBiz.updateModule(mid, moduleSaveForm);
        responseVo.setData(result);
        return responseVo;
    }


    /**
     * 删除模块
     *
     * @param mid
     * @return
     */
    @DeleteMapping("/pm/delete/{mid}")
    @Permission(value = {PermissionConstants.PERMISSION_DELETE_MODULE})
    public JsonResponse<Boolean> deleteModule(@PathVariable("mid") Integer mid) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.permissionBiz.deleteModule(mid);
        responseVo.setData(result);
        return responseVo;
    }


    /**
     * 获取角色列表
     *
     * @param roleQueryForm
     * @return
     */
    @PostMapping("/r/list")
    @Permission(value = {PermissionConstants.PERMISSION_GET_ROLE_LIST})
    public JsonResponse<PageDTO<RoleVo>> getRoleList(@Valid @RequestBody RoleQueryForm roleQueryForm) {
        JsonResponse<PageDTO<RoleVo>> responseVo = new JsonResponse<>();
        PageDTO<RoleVo> result = this.permissionBiz.getRoleList(roleQueryForm);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 添加权限
     *
     * @param permissionSaveForm
     * @return
     */
    @PostMapping("/p/add")
    @Permission(value = {PermissionConstants.PERMISSION_ADD_PERMISSION})
    public JsonResponse<Integer> addPermission(@Valid @RequestBody PermissionSaveForm permissionSaveForm) {
        JsonResponse<Integer> responseVo = new JsonResponse<>();
        Integer result = this.permissionBiz.addPermission(permissionSaveForm);
        responseVo.setData(result);
        return responseVo;
    }


    /**
     * 修改权限
     *
     * @param pid
     * @param permissionSaveForm
     * @return
     */
    @PutMapping("/p/update/{pid}")
    @Permission(value = {PermissionConstants.PERMISSION_UPDATE_PERMISSION})
    public JsonResponse<Boolean> updatePermission(@PathVariable("pid") Integer pid, @Valid @RequestBody PermissionSaveForm permissionSaveForm) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.permissionBiz.updatePermission(pid, permissionSaveForm);
        responseVo.setData(result);
        return responseVo;
    }


    /**
     * 批量修改权限所属模块
     *
     * @param pids
     * @param mid
     * @return
     */
    @PutMapping("/p/update/{pids}/{mid}")
    @Permission(value = {PermissionConstants.PERMISSION_UPDATE_PERMISSION_MODULE})
    public JsonResponse<Boolean> updatePermissionModule(@PathVariable("pids") String pids, @PathVariable("mid") Integer mid) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.permissionBiz.updatePermissionModule(pids, mid);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 添加角色
     *
     * @param roleSaveForm
     * @return
     */
    @PostMapping("/r/add")
    @Permission(value = {PermissionConstants.PERMISSION_ADD_ROLE})
    public JsonResponse<Integer> addRole(@Valid @RequestBody RoleSaveForm roleSaveForm) {
        JsonResponse<Integer> responseVo = new JsonResponse<>();
        Integer result = this.permissionBiz.addRole(roleSaveForm);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 修改角色
     *
     * @param rid
     * @param roleSaveForm
     * @return
     */
    @PutMapping("/r/update/{rid}")
    @Permission(value = {PermissionConstants.PERMISSION_UPDATE_ROLE})
    public JsonResponse<Boolean> updateRole(@PathVariable("rid") Integer rid, @Valid @RequestBody RoleSaveForm roleSaveForm) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.permissionBiz.updateRole(rid, roleSaveForm);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 修改角色拥有的菜单及权限
     *
     * @param rid
     * @param roleAuthSaveForm
     * @return
     */
    @PutMapping("/r/auth/{rid}")
    @Permission(value = {PermissionConstants.PERMISSION_CHANGE_ROLE_AUTH})
    public JsonResponse<Boolean> changeRoleAuth(@PathVariable("rid") Integer rid, @Valid @RequestBody RoleAuthSaveForm roleAuthSaveForm) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.permissionBiz.powerRoleAuth(rid, roleAuthSaveForm);
        responseVo.setData(result);
        return responseVo;
    }

}
